<?php
// 自己加的 ------
function y_getHost() {
	$proto = isset($_SERVER['SERVER_PORT']) && $_SERVER['SERVER_PORT'] == '443' ? 
		'https://' : 'http://';
	$host = isset($_SERVER['HTTP_HOST']) ? $_SERVER['HTTP_HOST'] : '';
	
	$dir = isset($_SERVER['SCRIPT_NAME']) ? dirname($_SERVER['SCRIPT_NAME']) : '';
	
	return rtrim($proto . $host . $dir, '/\\');
}
function y_listToTree($list, $pk='id', $pid = 'parentid', $child = '_child', $root='0') {  
    // 创建Tree  
    $tree = array();  
    if(is_array($list)) {  
        // 创建基于主键的数组引用  
        $refer = array();  
        foreach ($list as $key => $data) {  
            $refer[$data[$pk]] = & $list[$key];  
        }
        foreach ($list as $key => $data) {  
            // 判断是否存在parent  
            $parentId = $data[$pid];  
            if ($root == $parentId) {  
                $tree[] = & $list[$key];  
            }else{  
                if (isset($refer[$parentId])) {  
                    $parent = & $refer[$parentId];  
                    $parent[$child][] = & $list[$key];  
                }  
            }  
        }  
    }
	
    return $tree;  
}
// 编码单引号和html标签
function y_htmlspecialchars($string) {
	if (get_magic_quotes_gpc()) {
		$tmp = stripslashes($string);  // 加过了斜线就去掉
		$tmp = htmlspecialchars($tmp, ENT_QUOTES);
		return $tmp;
	} 
	return htmlspecialchars($string, ENT_QUOTES);
}
function y_stripslashes(& $con) {
	if(get_magic_quotes_gpc()) {
		if(is_array($con)) {
			foreach($con as $key => $val) {
				if(!is_array($val)) {
					$con[$key] = stripslashes($val);	
				}	else {
					y_stripslashes($val);
				}
			}	
		} else {
			return stripslashes($con);	
		}
	}
    
	return $con;
}
function y_header() {
	header("Content-Type:text/html; charset=utf-8");	
}
function y_getParam($key, $method = 'post') {
	$val = 'post' == $method ? 
		y_htmlspecialchars($_POST[$key]) :
		('get' == $method ? y_htmlspecialchars($_GET[$key]) : '');
	
	return $val;
}
/* 去掉index.php 只可在前台使用 后台仍然使用 tp 自带的 U() */
function y_u($url, $params = '', $redirect = false) {
	$url = rtrim($url, '/');
	if(strpos($url, '/') > 0) {
	} else {
		return __ROOT__ . '/' . $url;  // 如果只给出Action 则直接返回 不处理参数
	}
	
	$siteUrl = __ROOT__ . '/' . $url;
	
	$paramArr = null;
	if(is_string($params) && !empty($params)) {
		if(strpos($params, '&') > 0) {
			$paramArr = explode('&', $params);
		} else {
			$paramArr = array($params);
		}
		$tmp = null; $len = count($paramArr);
		for($i=0; $i<$len; $i++) {
			$tmp = explode('=', $paramArr[$i]);
			$siteUrl .= '/' . $tmp[0] . '/' . $tmp[1];
		}
	}
	
	if($redirect) {
		redirect($siteUrl);
		exit;
	}
	
	return $siteUrl;
}
// 得到全球通用头像
function y_gravatar($email, $s = 38, $d = 'mm', $r = 'g', $img = false, $atts = array()) {
	//$url = 'http://www.gravatar.com/avatar/';
	$url = 'https://secure.gravatar.com/avatar/';
	$url .= md5( strtolower( trim( $email ) ) );
	$url .= "?s=$s&d=$d&r=$r";
		if ( $img ) {
			$url = '<img src="' . $url . '"';
			foreach ( $atts as $key => $val )
				$url .= ' ' . $key . '="' . $val . '"';
			$url .= ' />';
		}

	return $url;
}
// 处理 gif 表情
function y_processFace($str) {
	// [haha]
	$pattern = "/\[(\w+)\]/U";
	$str = preg_replace($pattern, '<img src='. __ROOT__ . '/Public/images/smiles/icon_' . "\\1" . '.gif />', $str);
	
	return $str;
}
// 得到预览图片 新闻等用
function y_viewImage($path) {
	if(empty($path)) {
		return MY_CDN . '/images/nopic.jpg';
	}
	if(1 == strpos($path, 'ttp')) {
		return $path;
	}
	
	return __ROOT__ . '/' . $path;
}
// 内容分页
function y_content(&$result, $field = 'content') {
	import('ORG.Util.Page');
	$pageString = '';
	$p = 1;
	$flag = '[page]';
	if (isset($_GET['p'])) {
		$p = intval($_GET['p']);
	}
	
	$CONTENT_POS = strpos($result[$field], $flag);  //判断是否存在分页
	if($CONTENT_POS !== false) {
		$contents = array_filter( explode($flag, $result[$field]) );  //按分页标记分段
		$pagenumber = count( $contents );  //分页数
		
        $page = new Page( $pagenumber, 1 );
        $page->setConfig ( 'header', '页' );
		
		$result[$field] = $contents[$p - 1];  //将整个文章内容替换为分页内容
		
		$pageString = $page->show();
	}
	
	return $pageString;
}

// 关键字
function y_tags($key = '') {
	$r = '';
	if(!empty($key)) {
		if(strpos($key, '|')) {
			$arr = explode('|', $key);
			for($i=0; $i<count($arr); $i++) {
				$r .= '<a href="'.__ROOT__.'/Search?k='.$arr[$i].'" title="'. $arr[$i] .'">' . $arr[$i] . '</a>';
			}
		} else {$r = '<a href="'.__ROOT__.'/Search?k='.$key.'" title="'. $key .'">' . $key . '</a>';}
	}
	
	return $r;
}

